//
//  NSString+LILExtension.m
//  LILTools
//
//  Created by xl on 2023/11/4.
//

#import "NSString+LILExtension.h"

@implementation NSString (LILExtension)

- (BOOL)lil_containChinese {
    for (int i = 0; i < [self length]; i++) {
        unichar t_char = [self characterAtIndex:i];
        if (t_char >= 0x4E00 && t_char <= 0x9FA5) return YES;
    }
    return NO;
}

+ (NSString *)lil_formatTimestamp:(NSTimeInterval)timestamp {
    if (timestamp <= 0) return @"刚刚";
    
    NSInteger currentTime = [[NSDate date] timeIntervalSince1970];
    NSInteger difference = currentTime - timestamp;
    
    NSInteger oneMinute = 60;
    NSInteger oneHour = oneMinute * 60;
    NSInteger oneDay = oneHour * 24;
    NSInteger oneWeek = oneDay * 7;
    
    if (difference <= 0) return @"刚刚";
    
    if (difference <= oneMinute) return @"刚刚";
    
    // 1个小时内
    if (difference <= oneHour) {
        NSInteger minute = difference / oneMinute;
        return [NSString stringWithFormat:@"%zd分钟前", minute];
    }
    
    // 24小时内
    if (difference <= oneDay) {
        NSInteger hour = difference / oneHour;
        return [NSString stringWithFormat:@"%zd小时前", hour];
    }
    
    // 7天内
    if (difference <= oneWeek) {
        NSInteger day = difference / oneDay;
        return [NSString stringWithFormat:@"%zd天前", day];
    }
    
    NSDate *newDate = [NSDate date];
    NSDate *oldDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSInteger newYear = [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:newDate] year];
    NSInteger oldYear = [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:oldDate] year];
    NSInteger month = [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:oldDate] month];
    NSInteger day = [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:oldDate] day];
    
    // 今年内
    if (newYear == oldYear) {
        return [NSString stringWithFormat:@"%zd月%zd日", month, day];
    }

    return [NSString stringWithFormat:@"%zd年%zd月%zd日", oldYear, month, day];
}

+ (NSString *)lil_randomChinese {
    return [self lil_createRandomChineseWithNum:1];
}

+ (NSString *)lil_randomChar {
    return [self lil_createRandomWordWithNum:1];
}

+ (NSString *)lil_createRandomChineseWithNum:(NSUInteger)num {
    NSInteger _num = (NSInteger)num;
    if (_num <= 0) return @"";
    
    NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSMutableString *string = [NSMutableString string];
    
    do {
        NSInteger randomH = 0xA1 + arc4random() % (0xFE - 0xA1+1);
        NSInteger randomL = 0xB0 + arc4random() % (0xF7 - 0xB0+1);
        NSInteger number = (randomH<<8) + randomL;
        NSData *data = [NSData dataWithBytes:&number length:2];
        [string appendString:[[NSString alloc] initWithData:data encoding:gbkEncoding]];
        _num -= 1;
    } while (_num > 0);

    return [string copy];
}

+ (NSString *)lil_createRandomWordWithNum:(NSUInteger)num {
    NSInteger _num = (NSInteger)num;
    if (_num <= 0) return @"";
    
    NSMutableString *string = [NSMutableString string];
    
    do {
        int figure = (arc4random() % 26) + 97;
        char character = figure;
        [string appendString:[NSString stringWithFormat:@"%c", character]];
        _num -= 1;
    } while (_num > 0);

    return [string copy];
}

- (nullable NSString *)lil_substringToString:(NSString *)string {
    return [self lil_substringToString:string isContain:NO];
}

- (nullable NSString *)lil_substringToString:(NSString *)string isContain:(BOOL)isContain {
    if (![self containsString:string]) return nil;
    
    NSRange range = [self rangeOfString:string];
    NSString *t_string = [self substringToIndex:range.location];
    if (isContain) {
        t_string = [t_string stringByAppendingString:string];
    }
    return t_string;
}

- (nullable NSString *)lil_substringFromString:(NSString *)string {
    return [self lil_substringFromString:string isContain:NO];
}

- (nullable NSString *)lil_substringFromString:(NSString *)string isContain:(BOOL)isContain {
    if (![self containsString:string]) return nil;
    
    NSRange range = [self rangeOfString:string];
    NSString *t_string = [self substringFromIndex:range.location + range.length];
    if (isContain) {
        t_string = [string stringByAppendingString:t_string];
    }
    return t_string;
}

@end
